﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows.Ink;
using Kinect;

namespace PresentationProject
{
    /// <summary>
    /// GMouseControl.xaml 的交互逻辑
    /// </summary>
    public partial class GMouseControl : UserControl, INotifyPropertyChanged
    {
        public GMouseControl()
        {
            InitializeComponent();
            var p = System.Windows.Forms.Control.MousePosition;
            PrePoint = new Point(p.X, p.Y);

            
            目标位置 = new Point(80, 80);

            DataContext = this;

            flag = false;
        }

        Point PrePoint;

        public Boolean flag;

        public double X
        {
            get
            {
                return _X;
            }
            set
            {
                _X = value;
                OnPropertyChanged("X");
            }
        }
        private double _X;
        public double Y
        {
            get
            {
                return _Y;
            }
            set
            {
                _Y = value;
                OnPropertyChanged("Y");
            }
        }
        private double _Y;
        public Point 目标位置
        {
            get
            {
                return _目标位置;
            }
            set
            {
                _目标位置 = value;
            }
        }
        private Point _目标位置;
        /// <summary>
        /// 取值介于0-1之间，数值越大则在位移后产生的弹力越大
        /// </summary>
        

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

      

        public void test_MouseMove(object sender, MouseEvent e)
        {
            目标位置 = new Point(e.x, e.y);
            // Random newRandom=new Random();
            // 目标位置 = new Point(newRandom.Next(0, 100), newRandom.Next(0, 100));
            //目标位置 = new Point(_X, _Y);
            // Point newPoint = 目标位置;
            // 目标位置 = new Point(newPoint.X + 10, newPoint.Y + 10);
            //  drawLine(newPoint, 目标位置);
            if (flag == true)
                drawLine(PrePoint, 目标位置);


            //var X轴弹力 = (X - 目标位置.X) * -弹性系数;
            //x += X轴弹力;
            //x *= 1 - 流体摩擦力;
            //X += x;
            //var Y轴弹力 = (Y - 目标位置.Y) * -弹性系数;
            //y += Y轴弹力;
            //y *= 1 - 流体摩擦力;
            //Y += y;

            X = 目标位置.X;
            Y = 目标位置.Y;


            PrePoint = 目标位置;
        }
        public void beta_MouseMove(float x, float y)
        {
            目标位置 = new Point(x, y);
            // Random newRandom=new Random();
            // 目标位置 = new Point(newRandom.Next(0, 100), newRandom.Next(0, 100));
            //目标位置 = new Point(_X, _Y);
            // Point newPoint = 目标位置;
            // 目标位置 = new Point(newPoint.X + 10, newPoint.Y + 10);
            //  drawLine(newPoint, 目标位置);
            if (flag == true)
                drawLine(PrePoint, 目标位置);


            //var X轴弹力 = (X - 目标位置.X) * -弹性系数;
            //x += X轴弹力;
            //x *= 1 - 流体摩擦力;
            //X += x;
            //var Y轴弹力 = (Y - 目标位置.Y) * -弹性系数;
            //y += Y轴弹力;
            //y *= 1 - 流体摩擦力;
            //Y += y;

            X = 目标位置.X - 30;
            Y = 目标位置.Y - 30;


            PrePoint = 目标位置;
        }
        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            //var p = System.Windows.Forms.Control.MousePosition;
            //目标位置 = new Point(p.X, p.Y);
            //// Random newRandom=new Random();
            //// 目标位置 = new Point(newRandom.Next(0, 100), newRandom.Next(0, 100));
            ////目标位置 = new Point(_X, _Y);
            //// Point newPoint = 目标位置;
            //// 目标位置 = new Point(newPoint.X + 10, newPoint.Y + 10);
            ////  drawLine(newPoint, 目标位置);
            //if (flag == true)
            //    drawLine(PrePoint, 目标位置);


            ////var X轴弹力 = (X - 目标位置.X) * -弹性系数;
            ////x += X轴弹力;
            ////x *= 1 - 流体摩擦力;
            ////X += x;
            ////var Y轴弹力 = (Y - 目标位置.Y) * -弹性系数;
            ////y += Y轴弹力;
            ////y *= 1 - 流体摩擦力;
            ////Y += y;

            //X = 目标位置.X;
            //Y = 目标位置.Y;
            //PrePoint = 目标位置;
        }


        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            flag = !flag;
        }
        private void drawLine(Point from, Point to)
        {
            if (double.IsNaN(from.X) || double.IsNaN(from.Y)) return;
            if (double.IsNaN(to.X) || double.IsNaN(to.Y)) return;

            StylusPointCollection pts = new StylusPointCollection();

            pts.Add(new StylusPoint(from.X, from.Y));
            pts.Add(new StylusPoint(to.X, to.Y));




            Stroke newSt = new Stroke(pts);


            newSt.DrawingAttributes.Color = Colors.Red;
            myInkCanvas.Strokes.Add(newSt);
        }

    }
}
